package ca.etsmtl.log430.lab3;

import java.io.PipedReader;
import java.io.PipedWriter;

public class TauxFilter extends Thread{

	
	


	/**
	 * This class is intended to be a filter that will key on a particular taux
	 * provided at instantiation.  Note that the stream has to be buffered so that
	 * it can be checked to see if the specified severity appears on the stream.
	 * If this string appears in the input stream, teh whole line is passed to the
	 * output stream.
	 * 
	 * <pre>
	 * Pseudo Code:
	 *
	 * connect to input pipe
	 * connect to output pipe
	 *
	 * while not end of line
	 *
	 *		read input pipe
	 *
	 *		if specified severity appears on line of text
	 *			write line of text to output pipe
	 *			flush pipe
	 *		end if
	 *
	 * end while
	 * close pipes
	 * </pre>
	 *
	 * @author Nicolas Fayolle
	 * @version 1.0
	 */



		// Declarations

		boolean done;

		int taux;
		String comparaison="";
		PipedReader inputPipe = new PipedReader();
		PipedWriter outputPipe = new PipedWriter();

		public TauxFilter(int taux,String comparaison, PipedWriter inputPipe,PipedWriter outputPipe) {

			this.taux = taux;
			this.comparaison=comparaison;
			try {

				// Connect inputPipe
				this.inputPipe.connect(inputPipe);
				System.out.println("TauxFilter " + taux
						+ ":: connected to upstream filter.");

				// Connect outputPipe
				this.outputPipe = outputPipe;
				System.out.println("TauxFilter " + taux
						+ ":: connected to downstream filter.");

			} catch (Exception Error) {

				System.out.println("TauxFilter " + taux
						+ ":: Error connecting to other filters.");

			} // try/catch

		} // Constructor

		// This is the method that is called when the thread is started
		public void run() {

			// Declarations

			char[] characterValue = new char[1];
			// char array is required to turn char into a string
			String lineOfText = "";
			// string is required to look for the keyword
			int integerCharacter; // the integer value read from the pipe

			try {

				done = false;

				while (!done) {

					integerCharacter = inputPipe.read();
					characterValue[0] = (char) integerCharacter;

					if (integerCharacter == -1) { // pipe is closed

						done = true;

					} else {

						if (integerCharacter == '\n') { // end of line

							System.out.println("TauxFilter " + taux
									+ ":: received: " + lineOfText + ".");
							
							if ((this.comparaison.equals("<")) && (findTaux(lineOfText)<taux)) {
								System.out.println("TauxFilter "
										+ taux + ":: sending: "
										+ lineOfText + " to output pipe.");
								lineOfText += new String(characterValue);
								outputPipe
										.write(lineOfText, 0, lineOfText.length());
								outputPipe.flush();

							} // if
							else if ((this.comparaison.equals("=")) && (findTaux(lineOfText)==taux)) {
								System.out.println("TauxFilter "
										+ taux + ":: sending: "
										+ lineOfText + " to output pipe.");
								lineOfText += new String(characterValue);
								outputPipe
										.write(lineOfText, 0, lineOfText.length());
								outputPipe.flush();

							} // if
							
							else if ((this.comparaison.equals(">")) && (findTaux(lineOfText)>taux)) {
								System.out.println("TauxFilter "
										+ taux + ":: sending: "
										+ lineOfText + " to output pipe.");
								lineOfText += new String(characterValue);	
								outputPipe
										.write(lineOfText, 0, lineOfText.length());
								outputPipe.flush();

							} // if
							else
								System.out.println("mauvais argument de comparaison");
							
							
							lineOfText = "";


						} else {
							lineOfText += new String(characterValue);
						} // if //

					} // if

				} // while

			} catch (Exception error) {

				System.out.println("TauxFilter::" + taux
						+ " Interrupted.");

			} // try/catch

			try {

				inputPipe.close();
				System.out.println("TauxFilter " + taux
						+ ":: input pipe closed.");

				outputPipe.close();
				System.out.println("TauxFilter " + taux
						+ ":: output pipe closed.");

			} catch (Exception error) {

				System.out.println("TauxFilter " + taux
						+ ":: Error closing pipes.");

			} // try/catch

		} // run
		private int findTaux(String line){
			int taux=0;
			
			String[] tab=line.split(" ");
			
			taux=Integer.valueOf(tab[4]);
			
			System.out.print(taux);
			return taux;
		}
}
